import json
import time

import requests


def album_detail(album_id):
    # 专辑详情
    url = "http://musicapi.leanapp.cn/album/detail/dynamic?id=" + album_id
    content = requests.get(url)
    ret = json.loads(content.text)

    song_all = []

    album_name = ret['album']['name']  # 专辑名字
    album_desc = ret['album']['description']  # 专辑介绍
    album_company = ret['album']['company']  # 发片公司
    album_pic = ret['album']['picUrl']  # 图片
    song_id = ret['songs'][0]['id']  # 单曲id
    album_type = ret['album']['type']  # 类型
    album_time = ret['album']['publishTime']  # 出版时间
    name1 = ret['songs'][0]['ar'][0]['name']  # 名字1
    id1 = ret['songs'][0]['ar'][0]['id']  # id1
    album_id = ret['songs'][0]['al']['id']

    song_all.append(song_id)
    try:
        id2 = ret['songs'][0]['ar'][1]['id']
        name2 = ret['songs'][0]['ar'][1]['name']  # 名字2
    except Exception as s:
        id2 = ''
        name2 = ''
    try:
        all = ",".join(song_all)
    except Exception as e:
        all = song_all
    t = str(album_time)
    p = t[0:10]
    # print(new_user_id)
    format = '%Y-%m-%d'
    value = time.localtime(int(p))

    times = time.strftime(format, value)

    album_detail_list = []
    dicts = {
        'album_name': album_name,
        'album_desc': album_desc,
        'album_company': album_company,
        'album_pic': album_pic,
        'album_type': album_type,
        'name1': name1,
        'album_time': times,
        'id1': id1,
        'album_id': album_id,
        'id2': id2,
        'name2': name2
    }
    album_detail_list.append(dicts)
    return album_detail_list


def album_similar(id1):
    # 获取此歌手相关专辑
    sim_album_url = 'http://musicapi.leanapp.cn/artist/album?id=' + str(id1) + '&limit=3'
    content = requests.get(sim_album_url)
    ret = json.loads(content.text)
    i = 0
    b = 0
    album_lists = []
    try:
        while b <= 2:
            times = ret['hotAlbums'][b]['publishTime']  # 出版时间
            alb_name = ret['hotAlbums'][b]['name']  # 专辑名字
            alb_pic = ret['hotAlbums'][b]['picUrl']  # 专辑图片
            alb_id = ret['hotAlbums'][b]['id']  # 专辑id
            nickname = ret['hotAlbums'][b]['artists'][0]['name']  # 歌手名字

            t = str(times)
            p = t[0:10]
            # print(new_user_id)
            format = '%Y-%m-%d'
            value = time.localtime(int(p))

            timeas = time.strftime(format, value)

            i += 1
            b += 1
            dicts = {
                'timeas': timeas,
                'alb_name': alb_name,
                'alb_pic': alb_pic,
                'nickname': nickname,
                'alb_id': alb_id
            }
            album_lists.append(dicts)
    except Exception as e:
        print(e)

    return album_lists


def album_song(album_id):
    all_album_url = 'https://v1.hitokoto.cn/nm/album/' + str(album_id)
    content = requests.get(all_album_url)
    ret = json.loads(content.text)
    a = 0
    m = 0
    all_album_list = []
    all_song_id = []
    try:
        while a <= 100:
            singer_id = ret['songs'][m]['ar'][0]['id']  # 歌手id
            singer_name = ret['songs'][m]['ar'][0]['name']  # 歌手名字
            mv_id = ret['songs'][m]['mv']  # mv_id
            song_name = ret['songs'][m]['name']  # 歌曲名字
            song_ids = ret['songs'][m]['id']  # 歌曲id
            all_song_id.append(str(song_ids))
            a += 1
            m += 1
            try:
                singer_id2 = ret['songs'][0]['ar'][1]['id']  # 歌手id2
                singer_name2 = ret['songs'][0]['ar'][1]['name']  # 名字2
            except Exception as s:
                singer_id2 = ''
                singer_name2 = ''

            dic = {
                'singer_id': singer_id,
                'singer_name': singer_name,
                'mv_id': mv_id,
                'song_name': song_name,
                'song_id': song_ids,
                'num': m,
                'singer_id2': singer_id2,
                'singer_name2': singer_name2,
            }
            all_album_list.append(dic)

    except Exception as e:
        print(e)
    song_count = a
    all_id = ",".join(all_song_id)

    return all_id, all_album_list, song_count


def pay_money(count, money):
    if int(money) == 15:
        day = int(count) * 31
        return day
    elif int(money) == 45:
        day = int(count) * 93
        return day
    elif int(money) == 90:
        day = int(count) * 186
        return day
    else:
        day = int(count) * 372
        return day

